package com.junqson.je.admin.service;

import com.alibaba.fastjson.JSON;
import com.junqson.je.admin.client.UserClient;
import com.junqson.je.admin.model.UserAuthOrderModel;
import com.junqson.je.admin.model.vo.UserAuthExamineResult;
import com.junqson.je.admin.repo.UserAuthOrderRepo;
import com.junqson.je.common.exception.BizException;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.model.dto.AuthInfoDTO;
import com.junqson.je.common.model.dto.AuthResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/3 12:15
 * @Version 1.0
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    UserAuthOrderRepo userAuthOrderRepo;

    @Autowired
    UserClient userClient;


    public void createUserAuthOrder(AuthInfoDTO authInfoDTO) {
        UserAuthOrderModel authOrder = new UserAuthOrderModel();

        authOrder.setCompany(authInfoDTO.getCompany());
        authOrder.setTitle(authInfoDTO.getTitle());
        authOrder.setUserId(authInfoDTO.getUserId());
        authOrder.setBizTime(new Date());

        List<String> proves = authInfoDTO.getProveFiles();
        if (proves != null) {
            String proveFiles = JSON.toJSONString(authInfoDTO.getProveFiles());
            authOrder.setProverFiles(proveFiles);
        }

        userAuthOrderRepo.save(authOrder);

    }

    public AuthResultDTO getAuthResult(UserAuthOrderModel authOrder) {

        AuthResultDTO result = new AuthResultDTO();
        result.setUserId(authOrder.getUserId());
        result.setTitle(authOrder.getTitle());
        result.setResultMsg(authOrder.getResultMsg());
        result.setPass(authOrder.isPass());
        result.setExamine(authOrder.isExamine());
        result.setCompany(authOrder.getCompany());
        if (authOrder.isExamine()) {
            result.setExamineTime(authOrder.getExamineTime());
        }
        return result;
    }


    public ApiResult setUserAuthResult(UserAuthExamineResult result) {
        UserAuthOrderModel model = userAuthOrderRepo.getByOrderId(result.getOrderId());
        if (model == null) {
            log.error("user auth order id {} not found", result.getOrderId());
            throw new BizException("用户审核订单不存在");
        }
        if (model.isExamine()) {
            log.error("user auth order has examined! order id {}", result.getOrderId());
            throw new BizException("请勿重复审核");
        }


        model.setExamine(true);
        model.setPass(result.isPass());
        if (model.isPass()) {
            model.setResultMsg("认证通过");
        } else {
            if (StringUtils.isEmpty(result.getResultMsg())) {
                model.setResultMsg("认证失败");
            } else {
                model.setResultMsg(result.getResultMsg());
            }
        }
        model.setExamineTime(new Date());
        userAuthOrderRepo.save(model);

        AuthResultDTO authResultDTO = getAuthResult(model);
        ApiResult result1 = userClient.authResultCallback(authResultDTO);
        return result1;

    }


    public List<UserAuthOrderModel> getWaitExamineAuthOrders() {
        List<UserAuthOrderModel> userAuthOrderModels = userAuthOrderRepo.getByExamineFalseOrderByBizTimeAsc();
        return userAuthOrderModels;
    }


    public List<UserAuthOrderModel> getAllAuthOrders() {
        return userAuthOrderRepo.findAll();
    }
}
